package com.javasm.controller;

import com.alibaba.fastjson.JSON;
import com.javasm.entity.AdminUser;
import com.javasm.entity.ReturnCode;
import com.javasm.entity.ReturnResult;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

@WebServlet("/getUserMenus")
public class getUserMenusServlet  extends HttpServlet {

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        /* 允许跨域访问相关参数设置 */
        /* 允许跨域的主机地址 */
        resp.setHeader("Access-Control-Allow-Origin", "http://localhost:5173");
        /* 允许跨域的请求⽅法GET, POST, HEAD 等 */
        resp.setHeader("Access-Control-Allow-Methods", "*");
        /* 重新预检验跨域的缓存时间 (s) */
        resp.setHeader("Access-Control-Max-Age", "3600");
        /* 允许跨域的请求头 */
        resp.setHeader("Access-Control-Allow-Headers", "*");
        /* 是否携带cookie */
        resp.setHeader("Access-Control-Allow-Credentials", "true");



        HttpSession session = req.getSession();
        List<AdminUser> userMenus = (List<AdminUser>)session.getAttribute("userMenus");
        ReturnResult rrs = new ReturnResult();
        if (userMenus.size() > 0){
            rrs.setReturnCode(ReturnCode.QUERY_SUCCESS.getCode());
            rrs.setReturnMsg(ReturnCode.QUERY_SUCCESS.getMsg());
            rrs.setReturnData(userMenus);
        }else {
            rrs.setReturnCode(ReturnCode.QUERY_NO_DATA.getCode());
            rrs.setReturnMsg(ReturnCode.QUERY_NO_DATA.getMsg());


        }
        resp.setContentType("application/json;charset=utf-8");
        PrintWriter writer = resp.getWriter();
        writer.print(JSON.toJSONString(rrs));
        writer.close();


    }
}
